﻿// (c) Copyright slimCODE Software Inc. - www.slimcode.com
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SlimCode.Windows.Forms.Mobile;
using SlimCode.Windows.Controls.Mobile;
using System.Runtime.InteropServices;

namespace SlimCode.SlimPasswords
{
  internal partial class PasswordItemScreen : UserControl, IScreen
  {
    private const string HiddenPassword = "**********";

    private IScreenHost m_host;
    private PasswordItem m_item;

    public PasswordItemScreen()
    {
      InitializeComponent();

      popupButton.SetItem( new ItemView( ImageCache.Popup ) );

      userNameButton.Item.TextAlignment =
      emailButton.Item.TextAlignment =
      passwordButton.Item.TextAlignment = ContentAlignmentEx.MiddleLeft;
    }

    public PasswordItemScreen( PasswordItem item )
      : this()
    {
      m_item = item;
    }

    private void backMenuItem_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.HideScreen( DialogResult.Cancel );
      }
    }

    private void editMenuItem_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        m_host.ShowScreen( new EditPasswordItemScreen( m_item, false ) );
      }
    }

    private void deleteMenuItem_Click( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        if( MessageBox.Show(
          string.Format( "Do you really want to delete password entry '{0}'?", m_item.Title ),
          "Confirm",
          MessageBoxButtons.YesNo,
          MessageBoxIcon.Question,
          MessageBoxDefaultButton.Button1 ) == DialogResult.Yes )
        {
          Program.ActiveStore.DeletePasswordItem( m_item );
          Program.SaveStore( Program.ActiveStore, m_host );

          m_host.HideScreen( DialogResult.OK );
        }
      }
    }

    private void popupButton_Click( object sender, EventArgs e )
    {
      if( m_item != null )
      {
        this.ShowInfoForm();

        if( m_host != null )
        {
          this.MinimizeApplication();
        }
      }
    }

    private void ShowInfoForm()
    {
      PasswordInfoForm infoForm = new PasswordInfoForm( m_item, m_host );

      if( m_host != null )
      {
        m_host.HideScreen( DialogResult.OK );
      }

      infoForm.Show();
    }

    private void userNameButton_Click( object sender, EventArgs e )
    {
      if( m_item != null )
      {
        Clipboard.SetDataObject( m_item.UserName );

        if( Program.Settings.AutoMinimize )
        {
          this.MinimizeApplication();
        }
        else
        {
          messageLabel.Text = "User name copied to the clipboard!";
        }
      }
    }

    private void emailButton_Click( object sender, EventArgs e )
    {
      if( m_item != null )
      {
        Clipboard.SetDataObject( m_item.Email );

        if( Program.Settings.AutoMinimize )
        {
          this.MinimizeApplication();
        }
        else
        {
          messageLabel.Text = "Email copied to the clipboard!";
        }
      }
    }

    private void passwordButton_Click( object sender, EventArgs e )
    {
      if( m_item != null )
      {
        Clipboard.SetDataObject( m_item.Password );

        if( Program.Settings.AutoMinimize )
        {
          this.MinimizeApplication();
        }
        else
        {
          messageLabel.Text = "Password copied to the clipboard!";
        }
      }
    }

    private void showPasswordCheckBox_CheckStateChanged( object sender, EventArgs e )
    {
      if( showPasswordCheckBox.Checked )
      {
        passwordButton.Item.Text = m_item.Password;
      }
      else
      {
        passwordButton.Item.Text = HiddenPassword;
      }
    }

    private void urlButton_Click( object sender, EventArgs e )
    {
      if( m_item != null )
      {
        Clipboard.SetDataObject( m_item.Password );

        string url = m_item.UrlAddress;

        if( !string.IsNullOrEmpty( url ) )
        {
          try
          {
            if( !Uri.CheckSchemeName( url ) )
            {
              if( !url.StartsWith( "https://" ) )
              {
                url = "http://" + url;
              }
            }

            System.Diagnostics.Process.Start( url, string.Empty );
          }
          catch( Exception except )
          {
            System.Diagnostics.Debug.WriteLine( except.Message );
          }
        }
      }
    }

    private void UpdateInformation()
    {
      if( m_item != null )
      {
        titleLabel.Text = m_item.Title;
        categoryLabel.Text = m_item.Category;
        userNameButton.Text = m_item.UserName;
        emailButton.Text = m_item.Email;
        passwordButton.Item.Text = HiddenPassword;
        showPasswordCheckBox.Checked = false;

        urlLink.Text = m_item.UrlAddress;
        notesLabel.Text = m_item.Notes;

        if( !m_item.Active )
        {
          messageLabel.Text = "This entry is not active.";
        }
      }
    }

    private void AnyControl_GotFocus( object sender, EventArgs e )
    {
      if( m_host != null )
      {
        Program.ToggleInputPanelIf( m_host.MainForm.MainInputPanel, false );
      }
    }

    [ DllImport( "coredll.dll" ) ]
    private static extern int ShowWindow( IntPtr hWnd, int nCmdShow );

    private const int SW_MINIMIZED = 6;

    private void MinimizeApplication()
    {
      PasswordItemScreen.ShowWindow( m_host.MainForm.Handle, SW_MINIMIZED );
    }

    #region IScreen Members

    public Control Control
    {
      get { return this; }
    }

    public Control DefaultFocus
    {
      get { return popupButton; }
    }

    public MainMenu Menu
    {
      get { return mainMenu; }
    }

    public string Title
    {
      get { return "slimPASSWORDS - View"; }
    }

    public bool Hiding( DialogResult result )
    {
      return true;
    }

    public void HidingChild( IScreen child, DialogResult result )
    {
      if( result == DialogResult.OK )
      {
        EditPasswordItemScreen editScreen = child as EditPasswordItemScreen;

        if( editScreen != null )
        {
          if( editScreen.Changed )
          {
            Program.SaveStore( Program.ActiveStore, m_host );
            this.UpdateInformation();
          }
        }
      }
    }

    public void Showing( IScreenHost host )
    {
      m_host = host;

      this.UpdateInformation();
    }

    #endregion
  }
}
